import os
from wxpy import *

# CONF_PATH=os.path.join(os.path.dirname(__file__),'conf')
# IMG_PATH=os.path.join(os.path.dirname(__file__),'imgs')
# Cache_Path=os.path.join(CONF_PATH,'wxpy.pkl')
# bot=Bot(cache_path=Cache_Path,console_qr=True)
# @bot.register()
# def a():
#     pass
# bayin=bot.friends().search('巴音',sex=MALE)[0]
#
# bayin.send('已开通机器人服务,你收到的信息来自老张的机器人助手')
# bayin.send_image(IMG_PATH)
# bot.join()
# from wxpy.api import bot

class WXServer(object):
    def __init__(self):
        super(WXServer, self).__init__()
        self.CONF_PATH = os.path.join(os.path.dirname(__file__), 'conf')
        self.IMG_PATH = os.path.join(os.path.dirname(__file__), 'imgs')
        self.Cache_Path = os.path.join(self.CONF_PATH, 'wxpy.pkl')
        self.bot = Bot(cache_path=self.Cache_Path, console_qr=True)
        #微信对话平台
        self.APPID= 'gAN9RwevbV1fbx4'
        self.TOKEN= 'uEj3814l9YPZFQiNRpu0kymIANqxsu'
        self.EncodingAESKey= 'ue2gZyLMsOK53D5U5VKUL67v4x7zHMekMGbqWSoOBG'

    def get_friend(self,friend_name):
        """
        获取朋友聊天对象
        :param friend_name:
        :return:
        """
        fr=self.bot.friends().search(friend_name)[0]
        return fr

    def get_puid(self,friend):
        """
        获取朋友位于用户puid
        :param friend:
        :return:
        """
        return friend.puid

    def send_msg(self,friend,content):
        """
        给指定朋友发送信息
        :param friend:
        :param content:
        :return:
        """
        fr=self.bot.friends().search(friend)[0]
        fr.send(content)

    def send_img(self,friend,img_path):
        """
        给指定朋友发送图片
        :param friend:
        :param img_path:
        :return:
        """
        fr=self.bot.friends().search(friend)[0]
        fr.send_image(img_path)

    def send_to_self(self,msg):
        """
        给自己发送信息
        :param msg:
        :return:
        """
        self.bot.self.add()
        self.bot.self.accept()
        self.bot.self.send(msg)

    # @bot.register(msg_types=FRIENDS)
    # def auto_accept(self,msg):
    #     if 'wxpy' in msg.text.lower():
    #         new_frient=self.bot.accept_friend(msg.card)
    #         new_frient.send('我的小机器助手帮我接受了你的好友请求')
    #
    # @bot.register(msg_types=FRIENDS,except_self=False)
    # def get_message(self,msg):
    #     print(msg.type)

    def listening(self):
        embed()

